package com.css.zfzx.sjcj.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author Zyl
 * @Date 2021/3/16 17:12
 * @Version 1.0
 * @Describe:
 */
public class CharTypeUtil {
    public static void main(String[] args) {
        StringBuffer characters = new StringBuffer();

        StringBuffer letter = new StringBuffer();

        StringBuffer number = new StringBuffer();

        String str = "中国abc123";
        char[] strArr = str.toCharArray();
        for (char string : strArr) {
            // 判断是否为字母
            if ((string + "").matches("[a-z]") || (string + "").matches("[A-Z]")) {
                letter.append(string + " ");
            }
            // 判断是否为数字
            if ((string + "").matches("[0-9]")) {
                number.append(string + " ");
            }
            // 判断是否为汉字
            if (isChineseChar(string + "")) {
                characters.append(string + " ");
            }
        }
        System.out.println(characters.toString());
        System.out.println(letter.toString());
        System.out.println(number.toString());
    }
    //分离字符串和数字
    public static Double separateNum(String str) {
        StringBuffer number = new StringBuffer();
        char[] strArr = str.toCharArray();
        for (char string : strArr) {
            // 判断是否为数字
            if ((string + "").matches("[0-9]")) {
                number.append(string);
            }
        }
        String num = number.toString();
        if(num.length()==0){
            return 0.0000;
        }
        return Double.valueOf(number.toString());
    }

    /**
     * 判断是否为汉字
     *
     * @param str 字符串
     * @return
     */

    public static boolean isChineseChar(String str) {
        boolean temp = false;
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            temp = true;

        }
        return temp;

    }
}
